#ifndef _THREAD_H_
#define _THREAD_H_

#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <unistd.h>
#include <pthread.h>
#include <signal.h>
#include <string.h>
//#include <strstream>
#include <errno.h>
#include <algorithm>
using namespace std;

#define PTHREAD_OPT_ADD 1
#define PTHREAD_OPT_DEL -1
#define PTHREAD_MANAGER_SLEEP 1
#define MIN_WAIT_TASK_COUNTS 10
#define DEFAULT_ADD_THREAD_COUNTS 10
#define DEFAULT_DEL_THREAD_COUNTS DEFAULT_ADD_THREAD_COUNTS

class CTask				//CTask类可以利用所学的装饰模式对其进行功能性增强
{
    public:
                CTask(){}
                CTask(string name);
                virtual ~CTask(){}

    public:
                virtual void run()
                {
                    cout << "hello world" << endl;  
                }
                void setdata(void *data);

    protected:
                string	m_strTaskName;
                void    *m_ptrData;

};

//只有类内的static函数才可以作为pthread_create的回调函数
class CThreadPool
{
	public:
		CThreadPool() {}
		CThreadPool(int min_thrednum, int max_thrednum, int max_tasknum);	
		int AddTask(CTask *task);
		int StopAll();
		int getTaskSize();
		bool ThreadAlive(pthread_t tid);
		
	public:		

			static int Idle_ThreadNum;
			static int Busy_ThreadNum;
			static int Max_ThreadNum;
			static int Min_ThreadNum;
			static int Wait_ExitThreadNum;
			static int Max_TaskCounts;
			
			pthread_t ManagerThreadId;
			static pthread_t *pthread_id;
			
			static bool shutdown;      
			static vector<CTask *> m_vecTaskList;
			static pthread_mutex_t m_pthreadMutex;    
			static pthread_cond_t m_pthreadCond;
			
	protected:
			int CreatePool();
			static void *Thread_WorkerFunc(void *args);
			static void *Thread_ManagerFunc(void *args);
};

#endif

